package com.thti.mecool.client.util;

import java.io.ByteArrayOutputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

public class ImageUtil {
	
	/**
	 * Bitmap对象转换为字节数组
	 * @param bitmap
	 * @return
	 */
	public static byte[] bitmapConvertByteArray(Bitmap bitmap){
//		int size=bitmap.getHeight()*bitmap.getWidth()*4;
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		//将照片转为字节数组
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		if(bitmap.isRecycled())
		bitmap.recycle();
		return baos.toByteArray();
	}
	/**
	 * 根据newWidth压缩Bitmap
	 * @param bitmap
	 * @param newWidth
	 * @return
	 */
	public static Bitmap ResizeBitmap(Bitmap bitmap,int newWidth){
		int width=bitmap.getWidth();
		int height=bitmap.getHeight();
		System.out.println("bitmap的高度"+width);
		float temp=((float)height)/((float)width);
		int newHeight=(int)((newWidth)*temp);
		float scaleWidth=((float)newWidth)/width;
		float scaleHeight=((float)newHeight)/height;
		Matrix matrix=new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix,true);
		if(bitmap.isRecycled())
		bitmap.recycle();
		return resizedBitmap;
	}
	
	/**
	 * 图片旋转
	 * 
	 * @param bmp
	 * @param degree
	 * @return
	 */
	public static Bitmap postRotateBitamp(Bitmap bmp, float degree) {
		//获得Bitmap的高和宽
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		//产生resize后的Bitmap对象
		Matrix matrix = new Matrix();
		matrix.postRotate(degree);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		return resizeBmp;
	}
	
	public static HashMap<String, Drawable> imageCache = new HashMap<String, Drawable>();
	public static HashMap<String, SoftReference<Bitmap>> imageBitmap = new HashMap<String, SoftReference<Bitmap>>();
	/**
     * 判断屏幕大小返回不用尺寸图片大小
     * spacing: 长  parameters:高
     *  图片计算公式   长*高  
     *  （长/3+长）=高度
     * */
	 public static HashMap<String, Integer> winJudgmentSize(Activity mActivity){
		 	HashMap<String, Integer> map = new HashMap<String, Integer>();
	    	DisplayMetrics metrics = new DisplayMetrics();
	    	mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
			@SuppressWarnings("unused")
			int width = metrics.widthPixels;
			int height = metrics.heightPixels;
			// get the "i" page data
			switch (height) {
			case 854:
				map.put("homeGridViewWidth", 332);//首页GridView长
				map.put("homeGridViewHeight", 420);//首页GridView高
				map.put("homeGridViewSpacing", -55);//首页GridView间距
				map.put("shoppingGridViewWidth", 140);
				map.put("shoppingGridViewHeight", 140);
				break;
			case 480:
				map.put("homeGridViewWidth", 152);//首页GridView长
				map.put("homeGridViewHeight", 190);//首页GridView高
				map.put("homeGridViewSpacing", -25);//首页GridView间距
				map.put("shoppingGridViewWidth", 90);
				map.put("shoppingGridViewHeight", 90);
				break;
			case 800:
				map.put("homeGridViewWidth", 304);//首页GridView长
				map.put("homeGridViewHeight", 380);//首页GridView高
				map.put("homeGridViewSpacing", -50);//首页GridView间距
				map.put("shoppingGridViewWidth", 140);
				map.put("shoppingGridViewHeight", 140);
				break;
			case 320:
				map.put("homeGridViewWidth", 88);//首页GridView长
				map.put("homeGridViewHeight", 110);//首页GridView高
				map.put("homeGridViewSpacing", -10);//首页GridView间距
				map.put("shoppingGridViewWidth", 70);
				map.put("shoppingGridViewHeight", 70);
				break;
			case 432:
				map.put("homeGridViewWidth", 160);//首页GridView长
				map.put("homeGridViewHeight", 200);//首页GridView高
				map.put("homeGridViewSpacing", -10);//首页GridView间距
				map.put("shoppingGridViewWidth", 50);
				map.put("shoppingGridViewHeight", 50);
				break;
			case 400:
				map.put("homeGridViewWidth", 88);//首页GridView长
				map.put("homeGridViewHeight", 110);//首页GridView高
				map.put("homeGridViewSpacing", -10);//首页GridView间距
				map.put("shoppingGridViewWidth", 70);
				map.put("shoppingGridViewHeight", 70);
				break;
			case 640:
				map.put("homeGridViewWidth", 304);//首页GridView长
				map.put("homeGridViewHeight", 380);//首页GridView高
				map.put("homeGridViewSpacing", -50);//首页GridView间距
				map.put("shoppingGridViewWidth", 140);
				map.put("shoppingGridViewHeight", 140);
				break;
			default:
				map.put("homeGridViewWidth", 150);//首页GridView长
				map.put("homeGridViewHeight", 200);//首页GridView高
				map.put("homeGridViewSpacing", -20);//首页GridView间距
				map.put("shoppingGridViewWidth", 50);
				map.put("shoppingGridViewHeight", 50);
				break;
			}
			return map;
	    }
	 
	 
	 public static void add(String url,Drawable value){
		 imageCache.put(url, value);
	 }
	 
	 public static Drawable get(String key){
		return imageCache.get(key);
	 }
}
